﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KeuanganAdd : Form
    {
        private List<int> cat_id = new List<int>();

        public KeuanganAdd()
        {
            InitializeComponent();
        }

        private enum Category { PEMASUKAN, PENGELUARAN }
        private void loadKategori(Category category)
        {
            string sql;
            if (category == Category.PENGELUARAN)
                sql = "SELECT * FROM Expense_type";
            else
                sql = "SELECT * FROM Income_type WHERE id <> 1";

            cat_id.Clear();
            cmbKategori.Items.Clear();
            DataTable dt = Database.getInstance().query(sql);
            foreach (DataRow row in dt.Rows)
            {
                cat_id.Add((int)row[0]);
                cmbKategori.Items.Add(row[1].ToString());
                // Display selected index
                cmbKategori.SelectedIndex = 0;
            }
        }

        private void KeuanganAdd_Load(object sender, EventArgs e)
        {

        }

        private void cmbTipe_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbTipe.SelectedIndex == 0)
                loadKategori(Category.PENGELUARAN);
            else if (cmbTipe.SelectedIndex == 1)
                loadKategori(Category.PEMASUKAN);
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (true == this.checkFieldValue())
            {
                string command = "SELECT * FROM Finance";
                DataTable dt = Database.getInstance().query(command);

                DataRow dataRow = dt.NewRow();

                dataRow["receipt_no"] = this.receipt.Text;
                dataRow["time"] = this.dateTransaction.Value;
                dataRow["description"] = this.description.Text;
                dataRow["amount"] = decimal.Parse(this.amount.Text);
                
                if (this.cmbTipe.SelectedIndex == 0)
                {
                    dataRow["type"] = "PENGELUARAN";
                    if (this.cmbKategori.Items.Count > 0)
                    {
                        dataRow["expense_id"] = this.cat_id[this.cmbKategori.SelectedIndex];
                    }
                }
                else if (this.cmbTipe.SelectedIndex == 1)
                {
                    dataRow["type"] = "PEMASUKAN";
                    if (this.cmbKategori.Items.Count > 0)
                    {
                        dataRow["income_id"] = this.cat_id[this.cmbKategori.SelectedIndex];
                    }
                }

                dataRow["acc_id"] = Database.getInstance().user_id;
                
                dt.Rows.Add(dataRow);

                Database.getInstance().updateDB(dt);

                if (this.cmbTipe.SelectedIndex == 0)
                {
                    var result = MessageBox.Show("Tambah pengeluaran berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        this.Close();
                    }
                }
                else if (this.cmbTipe.SelectedIndex == 1)
                {
                    var result = MessageBox.Show("Tambah pemasukan berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        this.Close();
                    }
                }
            }      
        }

        private Boolean checkFieldValue()
        {
            if (this.description.TextLength == 0)
            {
                MessageBox.Show("Harap masukkan data keterangan !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (cmbTipe.SelectedIndex == -1)
            {
                MessageBox.Show("Pilih kategori !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            if (this.amount.Text == "")
            {
                MessageBox.Show("Harap masukkan data jumlah !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            return true;
        }

        private void amount_TextChanged(object sender, EventArgs e)
        {
            TextBox txtInput = (TextBox)sender;

            if (txtInput.TextLength != 0)
            {
                for (int i = 0; i < txtInput.TextLength; i++)
                {
                    if (Char.IsDigit(txtInput.Text, i))
                    {
                        // Do not do anything
                    }
                    else
                    {
                        MessageBox.Show("Harap gunakan angka !", "Peringatan!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        break;
                    }
                }
            }    
        }
    }
}
